package com.xp.controller;

import com.xp.service.RoleMenuService;
import com.xp.utils.Result;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色菜单关联控制器
 * 
 * @author xp
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/role-menus")
@RequiredArgsConstructor
public class RoleMenuController {

    private static final Logger log = LoggerFactory.getLogger(RoleMenuController.class);

    private final RoleMenuService roleMenuService;

    /**
     * 根据角色ID查询菜单ID列表
     * 
     * @param roleId 角色ID
     * @return 菜单ID列表
     */
    @GetMapping("/role/{roleId}")
    public Result<List<Long>> getMenuIdsByRoleId(@PathVariable Long roleId) {
        try {
            List<Long> menuIds = roleMenuService.getMenuIdsByRoleId(roleId);
            return Result.success(menuIds);
        } catch (Exception e) {
            log.error("根据角色ID查询菜单ID列表失败，角色ID: {}", roleId, e);
            return Result.error("查询菜单列表失败");
        }
    }

    /**
     * 根据菜单ID查询角色ID列表
     * 
     * @param menuId 菜单ID
     * @return 角色ID列表
     */
    @GetMapping("/menu/{menuId}")
    public Result<List<Long>> getRoleIdsByMenuId(@PathVariable Long menuId) {
        try {
            List<Long> roleIds = roleMenuService.getRoleIdsByMenuId(menuId);
            return Result.success(roleIds);
        } catch (Exception e) {
            log.error("根据菜单ID查询角色ID列表失败，菜单ID: {}", menuId, e);
            return Result.error("查询角色列表失败");
        }
    }

    /**
     * 为角色分配菜单
     * 
     * @param roleId 角色ID
     * @param menuIds 菜单ID列表
     * @return 操作结果
     */
    @PostMapping("/role/{roleId}/assign")
    public Result<Object> assignMenusToRole(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        try {
            boolean success = roleMenuService.assignMenusToRole(roleId, menuIds);
            if (success) {
                return Result.success("菜单分配成功");
            } else {
                return Result.error("菜单分配失败");
            }
        } catch (Exception e) {
            log.error("为角色分配菜单失败，角色ID: {}, 菜单IDs: {}", roleId, menuIds, e);
            return Result.error("菜单分配失败");
        }
    }

    /**
     * 删除角色的所有菜单关联
     * 
     * @param roleId 角色ID
     * @return 操作结果
     */
    @DeleteMapping("/role/{roleId}")
    public Result<Object> removeMenusByRoleId(@PathVariable Long roleId) {
        try {
            boolean success = roleMenuService.removeMenusByRoleId(roleId);
            if (success) {
                return Result.success("角色菜单关联删除成功");
            } else {
                return Result.error("角色菜单关联删除失败");
            }
        } catch (Exception e) {
            log.error("删除角色菜单关联失败，角色ID: {}", roleId, e);
            return Result.error("删除角色菜单关联失败");
        }
    }

    /**
     * 删除菜单的所有角色关联
     * 
     * @param menuId 菜单ID
     * @return 操作结果
     */
    @DeleteMapping("/menu/{menuId}")
    public Result<Object> removeRolesByMenuId(@PathVariable Long menuId) {
        try {
            boolean success = roleMenuService.removeRolesByMenuId(menuId);
            if (success) {
                return Result.success("菜单角色关联删除成功");
            } else {
                return Result.error("菜单角色关联删除失败");
            }
        } catch (Exception e) {
            log.error("删除菜单角色关联失败，菜单ID: {}", menuId, e);
            return Result.error("删除菜单角色关联失败");
        }
    }
}